package com.ymatou.iapi.trading.sellerquery.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery.parameter.GetOrderFlowsBean;
import com.ymatou.iapi.trading.sellerquery.parameter.resp.GetOrderFlowsResp;
import com.ymatou.iapi.trading.sellerquery.parameter.sp.Sp_GetOrderFlows;
import com.ymatou.query.seller.trading.api.parameter.FlowsDetail;
import com.ymatou.query.seller.trading.api.parameter.GetOrderFlowsResult;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

@TestSuite
public class Ts_GetOrderFlows {

	/**
	 * 获取订单资金流信息FreeMaker Template自动生成代码
	 */
	private static GetOrderFlowsBean getorderflowsbean;
	private static com.ymatou.query.seller.trading.api.service.GetOrderFlowsCall getorderflowscall;
//	private static com.ymatou.iapi.trading.sellerquery.service.GetOrderFlowsCall getorderflowscall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取订单资金流信息");
	}

	@Before
	public void caseUp() {
		getorderflowsbean = new GetOrderFlowsBean();
		getorderflowscall = new com.ymatou.query.seller.trading.api.service.GetOrderFlowsCall();
//		getorderflowscall = new com.ymatou.iapi.trading.sellerquery.service.GetOrderFlowsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getorderflowscall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetOrderFlowsResp expectResponse = new GetOrderFlowsResp();
		ConstructExpectedResponse(expectResponse);
		GetOrderFlowsResp actualResponse = getorderflowscall
				.getOrderFlowsResponse();
		DataUtil.verify(expectResponse.isIsShangouOrder(), actualResponse.isIsShangouOrder(), "验证IsShangouOrder");
		if(expectResponse.getFlows() == null){
			DataUtil.verify(null, actualResponse.getFlows(), "验证Flows==null");
		}else{
			boolean sameSize = expectResponse.getFlows().size()==actualResponse.getFlows().size();
			DataUtil.verify(expectResponse.getFlows().size(), actualResponse.getFlows().size(), "验证Flows.size()");
			if(sameSize){
				for(int i=0;i<expectResponse.getFlows().size();i++){
					FlowsDetail expect = expectResponse.getFlows().get(i);
					FlowsDetail actual = actualResponse.getFlows().get(i);
					verifyFlowsDetail(expect, actual, i);
				}
			}
		}
	}
	
	private void verifyFlowsDetail(FlowsDetail expect,FlowsDetail actual, int i){
		i += 1;
		DataUtil.verify(expect.getAccountPaidAmount(), actual.getAccountPaidAmount(),"验证Flows[" + i + "].AccountPaidAmount");
		DataUtil.verify(expect.getCouponChannel(), actual.getCouponChannel(),"验证Flows[" + i + "].CouponChannel");
		DataUtil.verify(expect.getCouponCode(), actual.getCouponCode(),"验证Flows[" + i + "].CouponCode");
		DataUtil.verify(expect.getEvidence(), actual.getEvidence(),"验证Flows[" + i + "].Evidence");
		DataUtil.verify(expect.getFreeCard(), actual.getFreeCard(),"验证Flows[" + i + "].FreeCard");
		DataUtil.verify(expect.isIsIncoming(), actual.isIsIncoming(),"验证Flows[" + i + "].IsIncoming");
		DataUtil.verify(expect.getOperation(), actual.getOperation(),"验证Flows[" + i + "].Operation");
		DataUtil.verify(expect.getPriceChange(), actual.getPriceChange(),"验证Flows[" + i + "].PriceChange");
		DataUtil.verify(expect.getSellerCouponCode(), actual.getSellerCouponCode(),"验证Flows[" + i + "].SellerCouponCode");
		DataUtil.verify(expect.getThirdPartyName(), actual.getThirdPartyName(),"验证Flows[" + i + "].ThirdPartyName");
		DataUtil.verify(expect.getThirdPartyPaidAmount(), actual.getThirdPartyPaidAmount(),"验证Flows[" + i + "].ThirdPartyPaidAmount");
		DataUtil.verifyDateEquals(expect.getTime(), actual.getTime(),"验证Flows[" + i + "].Time");
		DataUtil.verify(expect.getCash(), actual.getCash(),"验证Flows[" + i + "].Cash");
		DataUtil.verify(expect.getCoupon(), actual.getCoupon(),"验证Flows[" + i + "].Coupon");
		DataUtil.verify(expect.getFreight(), actual.getFreight(),"验证Flows[" + i + "].Freight");
		DataUtil.verify(expect.getGift(), actual.getGift(),"验证Flows[" + i + "].Gift");
		DataUtil.verify(expect.getSellerCoupon(), actual.getSellerCoupon(),"验证Flows[" + i + "].SellerCoupon");
		DataUtil.verify(expect.getTotal(), actual.getTotal(),"验证Flows[" + i + "].Total");


	}

	private void ConstructExpectedResponse(GetOrderFlowsResp response) {
		Sp_GetOrderFlows sp = new Sp_GetOrderFlows();
		sp.setOrderId(getorderflowsbean.getOrderId());
		sp.execute();
		GetOrderFlowsResult result = ConvertResult(sp);
		if(result.getOrderInfo() != null){
			response.setIsShangouOrder(result.getOrderInfo().getShangou());
			List<FlowsDetail> empty = new ArrayList<FlowsDetail>();
			response.setFlows(DataUtil.isNullOrEmpty(result.getFlows())==true?empty:result.getFlows());
		}
	}
	

	private GetOrderFlowsResult ConvertResult(
			Sp_GetOrderFlows sp) {
		GetOrderFlowsResult result = new GetOrderFlowsResult();
		result.setOrderInfo(sp.getOrderInfo());
		List<FlowsDetail> flows = null;
		if(false == DataUtil.isNullOrEmpty(sp.getFlows())){
			flows = new ArrayList<FlowsDetail>();
			for(com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail o : sp.getFlows()){
				FlowsDetail flow = new FlowsDetail();
				flow.setCash(o.getCash());
				flow.setCoupon(o.getCoupon());
				flow.setFreight(o.getFreight());
				flow.setGift(o.getGift());
				flow.setTotal(o.getTotal());
				flow.setCouponChannel(o.getCouponChannel());
				flow.setCouponCode(o.getCouponCode());
				flow.setEvidence(o.getEvidence());
				flow.setFreeCard(o.getFreeCard());
				flow.setIsIncoming(o.isIsIncoming());
				flow.setOperation(o.getOperation());
				flow.setPriceChange(o.getPriceChange());
				flow.setTime(o.getTime());
				flows.add(flow);
			}
		}
		result.setFlows(flows);
		return result;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFlows_001() {
		Logger.start(true, "根据OrderId获取订单资金流信息,成功");
		try {
			Logger.comment("造订单, 准备前置数据");
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int OrderId = o.create3_ShippedOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(1000l);
			Logger.comment("完成准备前置数据");

			getorderflowsbean.setOrderId(OrderId);
			String param = FormatUtil.beanToHttpString(getorderflowsbean, true,
					false);
			getorderflowscall.setData(param);
			getorderflowscall.callService();
			Logger.verifyEquals(ECode.SUCCESS, getorderflowscall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getorderflowscall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFlows_002() {
		Logger.start(true, "根据OrderId获取有退款退货订单信息,成功");
		try {
			Logger.comment("造订单, 准备前置数据");
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int OrderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			Thread.sleep(1000l);
			Logger.comment("完成准备前置数据");

			getorderflowsbean.setOrderId(OrderId);
			String param = FormatUtil.beanToHttpString(getorderflowsbean, true,
					false);
			getorderflowscall.setData(param);
			getorderflowscall.callService();
			Logger.verifyEquals(ECode.SUCCESS, getorderflowscall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getorderflowscall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFlows_003() {
		Logger.start(true, "订单商品申请退款中,根据orderid查询成功");
		try {
			Logger.comment("造订单, 准备前置数据");
			OrderCreate o = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int OrderId = o.create4_ApplyRefundOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(1000l);
			Logger.comment("完成准备前置数据");

			getorderflowsbean.setOrderId(OrderId);
			String param = FormatUtil.beanToHttpString(getorderflowsbean, true,
					false);
			getorderflowscall.setData(param);
			getorderflowscall.callService();
			Logger.verifyEquals(ECode.SUCCESS, getorderflowscall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getorderflowscall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderFlows_004() {
		Logger.start(false, "orderid不存在");
		try {
			int orderId = 11111111;
			String param = "OrderId=" + orderId;
			getorderflowscall.setData(param);
			getorderflowscall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getorderflowscall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getorderflowscall.getBCode(), "验证bcode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderFlows_005() {
		Logger.start(false, "orderid为null");
		try {
			String param = "";
			getorderflowscall.setData(param);
			getorderflowscall.callService();
			Logger.verifyEquals(ECode.ERROR400, getorderflowscall.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
